package com.bigknow.appstore.proxy.log;

import com.bigknow.appstore.proxy.enumerate.LogLevel;
import com.bigknow.appstore.proxy.log.impl.MongoDBWriter;
import org.mongodb.morphia.Datastore;

/**
 * 创建日志的类
 * Created by chaos on 2016/9/13.
 */
public final class LogFactory {

    /**
     * 根据类的名称来创建log
     *
     * @param clasz
     * @param props
     * @return
     */
    public static ILogWriter getLog(Class<? extends ILogWriter> clasz, LogLevel logLevel, Object... props) {
        //如果是mongoDBWriter 只有一个参数，就是Datastore
        if (clasz.equals(MongoDBWriter.class)) {
            try {
                MongoDBWriter mongoDBWriter = (MongoDBWriter) clasz.newInstance();
                mongoDBWriter.setLevel(logLevel);
                if (props!=null&&props[0] != null)
                    mongoDBWriter.setDs((Datastore) props[0]);
                return mongoDBWriter;
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException("create bean error [" + clasz + "]");
            }
        } else {
            throw new RuntimeException("not have this type [" + clasz + "]");
        }
    }

    public static void main(String[] args) {
        ILogWriter mongoDBWriter = LogFactory.getLog(MongoDBWriter.class, LogLevel.ERROR, null);
        System.out.println(mongoDBWriter);
    }
}
